package com.lightkits.wms.domain.purchasereceive;


import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.common.utility.validate.IntegrityValidator;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.domain.purchasereceive.service.PurHeadDomain;
import com.lightkits.wms.dto.purchasereceive.GetPurHeadRequestType;
import com.lightkits.wms.dto.purchasereceive.GetPurHeadResponseType;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 获取采购订单头
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/25 17:09
 */
@Component
public class PurcharseHeadExecutor implements ServiceExecutor<GetPurHeadRequestType, GetPurHeadResponseType> {
    @Autowired
    private PurHeadDomain purHeadDomain;

    @Override
    public GetPurHeadResponseType service(GetPurHeadRequestType request) {
        GetPurHeadResponseType response = new GetPurHeadResponseType();
        response.setSuccessful(true);
        try {

            IntegrityValidator.begin().validate(request.getPoId(), "Purcharse Order Id");
            response.setHeadDTO(purHeadDomain.getHead(request.getPoId()));

        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes= StringUtils.substringAfterLast(e.getMessage(),":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes)?e.getMessage():mes);

        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("get Purchase Head Error");
        }
        return response;
    }
}
